package GUI;

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;

import Control.Ctr;

import java.awt.Font;
import javax.swing.JPanel;
import javax.swing.JInternalFrame;
import javax.swing.Box;
import javax.swing.border.BevelBorder;
import java.awt.Component;
import javax.swing.JSeparator;
import javax.swing.SwingConstants;
import javax.swing.border.LineBorder;
import java.awt.Color;

public class Booking {

	private JFrame frame;
	private JTextField textDate;
	private JTextField textsStart;
	private JTextField textsEnd;
	private JTextField textcoID;
	private JTextField textClientssn;
	private JTextField textchID;
	private JTextField txtInputBookingId;
	private JTable table_1;
	private JTextField txtSingleUpdate;
	private Ctr ctr;
	private JTextField textSessions;
	private JTextField textPrice;
	private JTable table;
	private JTextField textField;
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Booking window = new Booking();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public Booking() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	private void initialize() {
		
		ctr =new Ctr();
		frame = new JFrame();
		frame.setBounds(100, 100, 688, 684);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.getContentPane().setLayout(null);
		
		final JLabel lblStatus = new JLabel("Status");
		lblStatus.setHorizontalAlignment(SwingConstants.CENTER);
		lblStatus.setFont(new Font("Arial", Font.BOLD, 14));
		lblStatus.setBounds(20, 605, 432, 23);
		frame.getContentPane().add(lblStatus);
		
		JLabel lblDate = new JLabel("Date");
		lblDate.setBounds(31, 31, 75, 23);
		frame.getContentPane().add(lblDate);
		
		JLabel lblStartHour = new JLabel("Start hour");
		lblStartHour.setBounds(31, 65, 75, 23);
		frame.getContentPane().add(lblStartHour);
		
		JLabel lblEndHour = new JLabel("End hour");
		lblEndHour.setBounds(31, 99, 75, 23);
		frame.getContentPane().add(lblEndHour);
		
		JLabel lblCourtId = new JLabel("Court ID");
		lblCourtId.setBounds(229, 31, 75, 23);
		frame.getContentPane().add(lblCourtId);
		
		JLabel lblClientId = new JLabel("Client SSN");
		lblClientId.setBounds(229, 65, 75, 23);
		frame.getContentPane().add(lblClientId);
		
		JLabel lblNewLabel = new JLabel("CoachID");
		lblNewLabel.setBounds(229, 99, 75, 23);
		frame.getContentPane().add(lblNewLabel);
		
		textDate = new JTextField();
		textDate.setText("2014-05-26");
		textDate.setBounds(92, 32, 100, 20);
		frame.getContentPane().add(textDate);
		textDate.setColumns(10);
		
		textsStart = new JTextField();
		textsStart.setText("10:00");
		textsStart.setBounds(116, 66, 50, 20);
		frame.getContentPane().add(textsStart);
		textsStart.setColumns(10);
		
		textsEnd = new JTextField();
		textsEnd.setText("11:00");
		textsEnd.setBounds(116, 100, 50, 20);
		frame.getContentPane().add(textsEnd);
		textsEnd.setColumns(10);
		
		textcoID = new JTextField();
		textcoID.setText("0");
		textcoID.setBounds(321, 32, 50, 20);
		frame.getContentPane().add(textcoID);
		textcoID.setColumns(10);
		
		textClientssn = new JTextField();
		textClientssn.setBounds(321, 66, 50, 20);
		frame.getContentPane().add(textClientssn);
		textClientssn.setColumns(10);
		
		textchID = new JTextField();
		textchID.setText("0");
		textchID.setBounds(321, 100, 50, 20);
		frame.getContentPane().add(textchID);
		textchID.setColumns(10);
		
		JButton btnCreateBooking = new JButton("Create Booking");
		btnCreateBooking.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
			
				//get the Court
				int coID = Integer.parseInt(textcoID.getText());
				//get the Date
				String date = textDate.getText();
				//get the Client
				Model.Client c = ctr.findBySSN(textClientssn.getText());
				//Get the Subscription Type
				//Model.Subscription sub = ctr.findBymID(c.getmID());
				//Get the Client ID
				int cID = c.getcID();
				//Get the Coach
				int chID = Integer.parseInt(textchID.getText());
				int l = 0;
				//Sessions to be added
		    	int s = 0;
		 
		    	
		    	int p =Integer.parseInt(textPrice.getText()); 

		    	int price = s * p - s*l;//Total Price, it will be 0 as the client may already bought sessions
				
				String sStart = textsStart.getText();//Session Start Time
				
				String sEnd = textsEnd.getText();//Session End Time
				Model.Booking bObj = ctr.findBookingbyDateTime(date, sStart, sEnd);
			if(bObj==null)
				{
					//Insert New Invoice into DB
				int iID= ctr.insertNewInvoice(date, cID, coID, chID, price);
					//Insert new Booking into DB
				int bID = ctr.insertNewBooking(coID, date, cID, chID, sStart, sEnd , iID);
				ctr.updateClient(cID, 4, "", s);//Add Sessions
				lblStatus.setText("Custom Booking created with ID:"+bID);}
				
			else
				lblStatus.setText("Error! Booking may be in conflict with another booking.");
				
	
		}
		});
		btnCreateBooking.setBounds(439, 27, 179, 32);
		frame.getContentPane().add(btnCreateBooking);
		
		txtInputBookingId = new JTextField();
		txtInputBookingId.setBounds(20, 178, 115, 20);
		txtInputBookingId.setText("Input Booking ID");
		frame.getContentPane().add(txtInputBookingId);
		txtInputBookingId.setColumns(10);
		
		JButton btnFindBooking = new JButton("Find Booking");
		btnFindBooking.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				int bID = Integer.parseInt(txtInputBookingId.getText());
				if(bID!=-1){
				Model.Booking b = ctr.findBybID(bID);
				TableModel model = new BookingTable(b);
				table_1.setModel(model);
				lblStatus.setText("Found booking!");
				}
				else
				{lblStatus.setText("Not found!");}
			}
		});
		btnFindBooking.setBounds(145, 177, 171, 23);
		frame.getContentPane().add(btnFindBooking);
		
		JButton btnDelete = new JButton("Delete Selected");
		btnDelete.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				int x = table_1.getSelectedRow();
				int y = 0;
				int cID = (int) table_1.getValueAt(x,y);
				boolean b =ctr.deleteBooking(cID);
				if(b)
					lblStatus.setText("Deleted!");
				else
					lblStatus.setText("Failed! Try again");
			}
		});
		btnDelete.setBounds(348, 208, 171, 23);
		frame.getContentPane().add(btnDelete);
		
		JButton btnListAll = new JButton("List ALL ");
		btnListAll.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
			TableModel model = new BookingTable();
			table_1.setModel(model);
			}
		});
		btnListAll.setBounds(254, 420, 171, 23);
		frame.getContentPane().add(btnListAll);
		
		JScrollPane scrollPane_1 = new JScrollPane();
		scrollPane_1.setBounds(10, 258, 649, 151);
		frame.getContentPane().add(scrollPane_1);
		
		table_1 = new JTable();
		table_1.setModel(new DefaultTableModel(
			new Object[][] {
				{null, null, null, null, null, null, null, null},
			},
			new String[] {
				"ID", "Date", "Client Name", "Coach", "Start Hour", "End Hour", "Invoice nr.", "Total Paid"
			}
		));
		table_1.getColumnModel().getColumn(0).setPreferredWidth(43);
		scrollPane_1.setViewportView(table_1);
		
		JButton btnNewButton = new JButton("Close");
		btnNewButton.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				frame.dispose();
				
			}
		});
		btnNewButton.setBounds(517, 606, 140, 23);
		frame.getContentPane().add(btnNewButton);
		
		final JComboBox comboBox = new JComboBox();
		comboBox.setModel(new DefaultComboBoxModel(new String[] {"Date", "Client ID", "Coach ID", "Star hour", "End hour", "Court ID"}));
		comboBox.setBounds(536, 209, 115, 20);
		frame.getContentPane().add(comboBox);
		
		txtSingleUpdate = new JTextField();
		txtSingleUpdate.setText("Single Update");
		txtSingleUpdate.setBounds(536, 178, 115, 20);
		frame.getContentPane().add(txtSingleUpdate);
		txtSingleUpdate.setColumns(10);
		
		textSessions = new JTextField();
		textSessions.setText("0");
		textSessions.setBounds(92, 133, 86, 20);
		frame.getContentPane().add(textSessions);
		textSessions.setColumns(10);
		
		JLabel lblSessions = new JLabel("Sessions");
		lblSessions.setBounds(31, 133, 46, 14);
		frame.getContentPane().add(lblSessions);
		
		textPrice = new JTextField();
		textPrice.setText("100");
		textPrice.setBounds(285, 133, 86, 20);
		frame.getContentPane().add(textPrice);
		textPrice.setColumns(10);
		
		JLabel lblPrice = new JLabel("Price");
		lblPrice.setBounds(229, 136, 46, 14);
		frame.getContentPane().add(lblPrice);
		
		JButton btnCreateCustomBooking = new JButton("Create Custom Booking");
		btnCreateCustomBooking.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				//get the Court
				int coID = Integer.parseInt(textcoID.getText());
				//get the Date
				String date = textDate.getText();
				//get the Client
				Model.Client c = ctr.findBySSN(textClientssn.getText());
				//Get the Subscription Type
				//Model.Subscription sub = ctr.findBymID(c.getmID());
				//Get the Client ID
				int cID = c.getcID();
				//Get the Coach
				int chID = Integer.parseInt(textchID.getText());
				int l = 0;
				//Sessions to be added
		    	int s = Integer.parseInt(textSessions.getText());
		    	//Price / Session (if customPrice)
		    	
		    	int p =Integer.parseInt(textPrice.getText()); 
		    	if(s>=10){
		    	l = 10;}
		    	int price = s * p - s*l;//Total Price
				
				String sStart = textsStart.getText();//Session Start Time
				
				String sEnd = textsEnd.getText();//Session End Time
				Model.Booking bObj = ctr.findBookingbyDateTime(date, sStart, sEnd);
			if(bObj ==null)
				{
					//Insert New Invoice into DB
				int iID= ctr.insertNewInvoice(date, cID, coID, chID, price);
					//Insert new Booking into DB
				int bID = ctr.insertNewBooking(coID, date, cID, chID, sStart, sEnd , iID);
				ctr.updateClient(cID, 4, "", s);//Add Sessions
				lblStatus.setText("Custom Booking created with ID:"+bID);}
				
			else
				lblStatus.setText("Error! Custom booking may be in conflict with another booking.");
				
	
		}
			
		});
		btnCreateCustomBooking.setBounds(439, 65, 179, 32);
		frame.getContentPane().add(btnCreateCustomBooking);
		
	
		
		JButton btnSingleUpdate = new JButton("Update Selected Booking");
		btnSingleUpdate.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				boolean r=false;
				//Get the cID from the row selected
				int x = table_1.getSelectedRow();
				int y = 0;
				int cID = (int) table_1.getValueAt(x,y);
				//Update Switch
				switch(comboBox.getSelectedIndex())
				{case 0: //Date
					r=ctr.updateBooking(cID,0,txtSingleUpdate.getText(),-1);
					break;
				case 1: //cID
					r=ctr.updateBooking(cID, 1,"", Integer.parseInt(txtSingleUpdate.getText()));
					break;
				case 2: //chID
					r=ctr.updateBooking(cID, 2,"", Integer.parseInt(txtSingleUpdate.getText()));
					break;
				case 3: //StartHour
					r=ctr.updateBooking(cID, 1, txtSingleUpdate.getText(), 0);
					break;
				case 4: //End hour
					r=ctr.updateBooking(cID, 2, txtSingleUpdate.getText(), 0);
					break;
				case 5: //court ID
					r=ctr.updateBooking(cID, 0, "", Integer.parseInt(txtSingleUpdate.getText()));
					break;
				}
				if(r)
					lblStatus.setText("Success!");
				else
					lblStatus.setText("Failed to update!");
			}
		});
		btnSingleUpdate.setBounds(348, 178, 171, 22);
		frame.getContentPane().add(btnSingleUpdate);
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(10, 474, 649, 120);
		frame.getContentPane().add(scrollPane);
		
		table = new JTable();
		DateFormat dateF = new SimpleDateFormat("yyyy-MM-dd");
		Date date = new Date();
		String s = dateF.format(date);
		table.setModel(new BookingTable(s));
		table.getColumnModel().getColumn(0).setPreferredWidth(43);
		scrollPane.setViewportView(table);
		
		JButton btnListByDate = new JButton("List by date");
		btnListByDate.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				TableModel model = new BookingTable(textField.getText());
				table_1.setModel(model);
			}
		});
		btnListByDate.setBounds(145, 211, 171, 23);
		frame.getContentPane().add(btnListByDate);
		
		textField = new JTextField();
		textField.setBounds(20, 209, 115, 20);
		frame.getContentPane().add(textField);
		Date date1= new Date();
      	String d = dateF.format(date1);
      	textField.setText(d);
		textField.setColumns(10);
		
		JLabel lblSessionsBookedToday = new JLabel("Sessions Booked today:");
		lblSessionsBookedToday.setBounds(13, 459, 179, 14);
		frame.getContentPane().add(lblSessionsBookedToday);
		
		Box horizontalBox = Box.createHorizontalBox();
		horizontalBox.setBorder(new LineBorder(new Color(0, 0, 0)));
		horizontalBox.setBounds(10, 11, 649, 156);
		frame.getContentPane().add(horizontalBox);
		
		Box horizontalBox_1 = Box.createHorizontalBox();
		horizontalBox_1.setBorder(new LineBorder(new Color(0, 0, 0)));
		horizontalBox_1.setBounds(10, 172, 649, 80);
		frame.getContentPane().add(horizontalBox_1);
		

	}
}
